package net.huashitong.Project.util;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;


public class FileUploadUtil {
	/**
	 * 上传单个文件
	 * @param request
	 * @param response
	 * @return fileName
	 */
	public static String upload(HttpServletRequest request,HttpServletResponse response){
		String path=request.getSession().getServletContext().getRealPath(File.separator)+"webimages"+File.separator;
		CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
		commonsMultipartResolver.setDefaultEncoding("UTF-8");
		if (commonsMultipartResolver.isMultipart(request))  {
		    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		    MultipartFile file = multipartRequest.getFile("file");
		    String myFileName = file.getOriginalFilename(); 
		    path = path + myFileName;
		    File imageFile = new File(path);
		    try {
				file.transferTo(imageFile);
			} catch (IllegalStateException | IOException e) {e.printStackTrace();}
		}
		return path;
	}
	/**
	 * 上传多个文件
	 * @param request
	 * @param response
	 * @return fileName
	 */
	public static String imageUploadMore(HttpServletRequest request,HttpServletResponse response){
		String path=null;
		String fileName=null;
		StringBuffer imgurls=new StringBuffer();
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());  
        if(multipartResolver.isMultipart(request)){    
            MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;  
            Iterator<String> iter = multiRequest.getFileNames();  
            while(iter.hasNext()){
                MultipartFile file = multiRequest.getFile(iter.next());  
                if(file != null){  
                    String myFileName = file.getOriginalFilename();
                    if(myFileName.trim() !=""){ 
                    	fileName = myFileName;
                        imgurls.append(fileName);
                        path=request.getSession().getServletContext().getRealPath(File.separator)+"webimages"+File.separator+"article";
                        File checkFile=new File(path);
                        if(!checkFile.isDirectory()){
                        	checkFile.mkdirs();
                        }
                        path=path+File.separator+fileName;
                        System.out.println(path);
                        File localFile = new File(path);  
                        try {
							file.transferTo(localFile);
						} catch (IllegalStateException e) {e.printStackTrace();
						} catch (IOException e) {e.printStackTrace();}  
                    }  
                }  
            }       
        }
        return imgurls.toString();
	}
}
